package com.project.service.impl;

import com.project.bean.ClassBean;
import com.project.mapper.IClassDao;
import com.project.mapper.IStudentDao;
import com.project.service.IClassService;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * 班级业务接口实现类
 */
public class ClassServiceImpl extends BaseService implements IClassService {

    @Override
    public void addClass(ClassBean classBean) {
        SqlSession session = this.getSession();
        IClassDao classDao = session.getMapper(IClassDao.class);
        classDao.addClass(classBean);

        session.commit();
        session.close();
    }

    @Override
    public boolean delClass(Integer id) {
        SqlSession session = this.getSession();
        IClassDao classDao = session.getMapper(IClassDao.class);
        IStudentDao studentDao = session.getMapper(IStudentDao.class);
        if (studentDao.countByClass(id)==0){
            classDao.delClass(id);
            session.commit();
            session.close();
            return true;
        }
        return false;
    }

    @Override
    public List<ClassBean> findAllClass() {
        SqlSession session = this.getSession();
        IClassDao classDao = session.getMapper(IClassDao.class);
        List<ClassBean> list = classDao.findAllClass();

        session.close();
        return list;
    }

}
